package com.argrace.smart;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.argrace.smart.activity.MainActivity;
import com.argrace.smart.event.EventBusEvent;
import com.taobao.agoo.TaobaoBaseIntentService;

import org.android.agoo.common.AgooConstants;
import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.atomic.AtomicInteger;

public class TaobaoIntentService extends TaobaoBaseIntentService {
    private final static String TAG = "TaobaoIntentService";

    static AtomicInteger notificationIndex = new AtomicInteger(1);

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate()");
    }

    @Override
    protected void onError(Context context, String errorId) {
        Log.e(TAG, "onError: " + errorId);
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.e(TAG, "onRegistered: " + registrationId);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.e(TAG, "onUnregistered: " + registrationId);
    }

    @Override
    protected void onMessage(final Context context, final Intent intent) {
        final String message = (null != intent ? intent.getStringExtra(AgooConstants.MESSAGE_BODY) : "");
        if (TextUtils.isEmpty(message)) {
            return;
        }
//        Intent intent_b = new Intent();
//        intent_b.setAction("com.example.broadcast");
//        intent_b.putExtra("msg", message);
//        intent_b.putExtra("text", "1");
//发送广播  
//        sendBroadcast(intent_b);
        Log.i("info","TaobaoIntentService中MSG = " +message );
        try {
            JSONObject jsonObject = JSON.parseObject(message);

            String title = jsonObject.getString("title");
            String ticker = jsonObject.getString("ticker");
            String text = jsonObject.getString("text");


            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle(title);
            builder.setTicker(ticker);
            builder.setContentText(text);


            Intent intent1 = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            builder.setContentIntent(pendingIntent);


            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notificationIndex.getAndIncrement(), builder.build());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}